package com.example.demo.rabbitMq;

import com.example.demo.utils.date.DateUtils;
import com.example.demo.utils.json.JSONUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * RabbitMq 消息消费者
 */
@Component
public class MessageReceiver {

    /** 监听直连交换机 */
    @RabbitListener(queues = RabbitMqConstants.DirectQueue)
    @RabbitHandler
    public void directQueueProcess(MessageData messageData, Channel channel, Message message) {
        System.out.println(messageData.getMessageContent());
    }

    /** 主题交换机 */
    @RabbitListener(queues = RabbitMqConstants.TopicQueue)
    @RabbitHandler
    public void topicQueueProcess(MessageData messageData, Channel channel, Message message) throws Exception {
        System.out.println("接收时间：" + DateUtils.nowTime());
        System.out.println("通配队列：" + JSONUtils.toJSONObject(messageData).toJSONString());

        /**
         * basic.ack用于肯定确认   是否批量true：将一次性ack所有小于deliveryTag的消息；确认收到消息
         * basic.nack用于否定确认（注意：这是AMQP 0-9-1的RabbitMQ扩展）
         * basic.reject用于否定确认，但与basic.nack相比有一个限制:一次只能拒绝单条消息   第二个参数为true时，会重新放回队列
         */
        /**
         * 当一个消费者向 RabbitMQ 注册后，会建立起一个 Channel ，RabbitMQ 会用 basic.deliver 方法向消费者推送消息，这个方法携带了一个 delivery tag，
         * 它代表了 RabbitMQ 向该 Channel 投递的这条消息的唯一标识 ID，是一个单调递增的正整数，delivery tag 的范围仅限于 Channel
         */
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
//        channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
    }
    @RabbitListener(queues = RabbitMqConstants.TopicQueueFirst)
    @RabbitHandler
    public void topicQueueFirstProcess(MessageData messageData, Channel channel, Message message) {
        System.out.println("主题队列一：" + messageData.getMessageContent());
    }
    @RabbitListener(queues = RabbitMqConstants.TopicQueueSecond)
    @RabbitHandler
    public void topicQueueSecondProcess(MessageData messageData, Channel channel, Message message) {
        System.out.println("主题队列二：" + messageData.getMessageContent());
    }

    /** 死信队列监听 */
    @RabbitListener(queues = RabbitMqConstants.SX_Queue)
    @RabbitHandler
    public void sxQueueProcess(MessageData messageData, Channel channel, Message message) throws Exception {
        System.out.println("接收时间：" + DateUtils.nowTime());
        System.out.println("死信队列：" + JSONUtils.toJSONObject(messageData).toJSONString());

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

    /** 延迟消息监听 */
    @RabbitListener(queues = RabbitMqConstants.Delayed_Queue)
    @RabbitHandler
    public void delayedQueueProcess(MessageData messageData, Channel channel, Message message) throws Exception {
        System.out.println("接收时间：" + DateUtils.nowTime());
        System.out.println("延迟队列：" + JSONUtils.toJSONObject(messageData).toJSONString());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

}
